<?php

namespace App\Http\Controllers\App;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Pay;

class PayController extends Controller
{

    public $payType  = ['alipayapp' => 1, 'wxpayapp' => 2, 'balance' => 3, 'sdpay' => 4];
    
    public $payName  = [0 => '异常操作', 'alipayapp' => '支付宝', 'wxpayapp' => '微信' , 'balance' => '余额', 'sdpay' => '衫德'];

    public $payClass = [0 => '异常操作', 1 => 'alipayapp', 2 => 'wxpayapp' , 3 => 'balance', 4 => 'sdpay'];


    public function payOne(Request $request) 
    {
        $class    = $request->input('class');//

        $platform = $request->input('platform', 0); // 0 队列  1 系统

        if ($class == null) {
            return self::error('请确认支付类型');
        }

        $type   = isset($this->payType[$class]) ? $this->payType[$class] : 0;

        $payModel = Pay::where('status', Pay::STATUS_BEGIN)
                    ->where('platform', $platform)
                    ->where('type', $type)
                    ->first();

        if (empty($payModel)) return self::json([]);

        $params = [];
        if (!empty($payModel->params)) {
            $params = unserialize($payModel->params);
        }

        return self::json($params);
    }

    /**
     * 支付列表
     * @param  Request $request [description]
     * @return [type]           [description]
     */
    public function payList(Request $request) 
    {
        $payModel = Pay::get();

        $data = [];

        foreach ($payModel as $key => $value) {

            $data[$key]['name']     = $value->name;
            $data[$key]['status']   = $value->status;
            $data[$key]['icon']     = $value->icon;
            $data[$key]['class']    = $this->payClass[$value->type];
            $data[$key]['platform'] = $value->platform;
            $data[$key]['params']   = empty($value->params)   ? '' : unserialize($value->params);
        }

        return self::json($data);
    }
    /**
     * 添加/更新配置数据
     * @return [type] [description]
     */
    public function paySave(Request $request) 
    {
        $class  =  $request->input('class');//

        $platform = $request->input('platform', 0); // 0 队列  1 系统

        if ($class == null) {
            return self::error('请确认支付类型');
        }

        $type   = isset($this->payType[$class]) ? $this->payType[$class] : 0;

        $params = [];

        if ($class == 'wxpayapp') {

            $params['appid'] = $request->input('appid', 0);//公众账号ID
            //$params['appid'] = 'wxbe0ac038b36bd5b7';//公众账号ID
            $params['mchid'] = $request->input('mchid', 0);//商户号
            //$params['mchid'] = '1485379482';//商户号
            $params['key']   = $request->input('key', 0);  //支付密钥
            //$params['key']   = 'OBgEPnGxoPfXqFy1G8j2U5c2vxzuQENU';  //支付密钥
        } 

        if ($class == 'alipayapp') {

            //$params['appid']       = $request->input('appid', 0);
            $params['appid']               = '2017071007700291';
            //商户私钥，您的原始格式RSA私钥
            $params['private_key'] = $request->input('private_key',0);
            //$params['private_key'] = 'MIIEpQIBAAKCAQEA4HuYdB6X9m7Z8C4EZ9L701P3RL9EFeyufKh+U8f6FDpsn/caCjz0ZoEcNWXR2OYfeAbAk2wWqjIYG8RP5E0A8KP+zh1mIcSsq226+3pGaKY733UXbnzO0nJiscute6lgL7sVGIR0D8dP/tDcp4Yz1KvVMHWTay2o/il7t+zm2ul4HSu18KB30NPdgMbbrWKHCcQkZP064qPtXFEaaJcyiM5udm2vCyYeCFbinTETqtRD6MjgZHVLuG2rzOg35WYplLGZO6Um/Alb63bQbbLsRgaV2nVGQ/bIugXNbsHyLp7HUQbnAl0wjdaTjY06roe0WWBdPjBeSQxw8YaFECwxQQIDAQABAoIBAQCnfG+9y4mz6vceFSdiU29iOBaYK2BgP0vcZFaEqgKzDvg2zcWW9+2dgR0L1iEghGncQqVyGMY8S83jAbBd9PoLkUAuho7KCRSPfvpwYMWhlM29Jn7KB8LED2YFo6KXTkMeuaW+uixiDWJpC2pmb2HCfkY8tqy73cBliYvuKYoAr6UB8Ylcc9+qCnteLattWQC+YU/JrVc9of/qXLGbTqfOgrPeQMED8KKcEW7An42qz0nn0lEXw+6Jfi5uL9M1CG5U2/IE8s6UBJjXNY9Ej83+uKfmGjRfO7buWi1UlCXxkI2HE35YGJw9csjEAIV3efV5wsNL0RWPEDveeS/RMIrVAoGBAPXa71QfGl6GUhu8J9dD2xjagtNkujjHXeU9S9IUwhFVeAVxyVgE0n87mBY/nFZWpsOjlGL+yI7rvBGnT22yzuAnuj6ovKn6hlBhDTPAPYAZhKOaaoTXlMkM/9PljJBezghYs3mqCFCgX+3gvo4oJZ4l+FoB2bnMqYKx0GCsXfQrAoGBAOm+5ThRFd6/iP1/EUH/TG+2GwHl4nircS9eqlToikhPDou8JI5JdpXWknW3wsS9iX19XXKTMShEMK8HgdHU61yaDDET8BZld5B7O5WT3gD+ywfSWO3y70BkN814V61UfveRUpkENoyJTD+/DTBCrkTG+QO9kcUuR3iJGN3JiN5DAoGBAMydujI48KlbAVsz7bSrNN4qfleRraszwmHX8hKWVC45A/sECJ7I5Kgf4SQjLOpuk7yfmBAMHL9WUuAeb2zNNIA6kDc2GE8fwkmDfIllpcnHcyz2h1CoW5w4xXKFycceIDuyFy1a5jC0SSKhufZ2U1icIWZHw/1u6GVtuA+9ZTShAoGBAMiyhr//J0cVr/rwspG3MhstAi10BCtWER36K0gyTiFd+ABU8BJX04E+Nozq1kn07+uWy7jH10pamm5+hKH1Lo1ZG6S8GMYyIyIkHXoF93VlUFCL9J2CnJjuM9exP57s0QOifIgzzN6IOLb3RMyuRFF7gm4Kw/HJFcbR+E7eby1fAoGAJlbnbPWBiz9T3dD2yftOnS8AKba0SKM3DRF/5NKEXMtuYv6CRT4YvrzJND6z1Ug13ZnpCu+2CSzfL7/0v5HRVln7tNPsNnCDa8VKNGMFO12mxG/Ix+aTo5ucRczumO9A26QblTerRGqTeROgeRgYcxUi/tzUDDknnroxORBGLbU=';
            //支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
            $params['public_key']    = $request->input('public_key',0);
            //$params['public_key']    = 'MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEApSflPxoMW1BmoWmBbx1R4MqYrvYh0GVzIdoPSlSb1gfaKL95CFcgIyY8JM77Y3PSFCdIWLFpnHppFkeBTosFdGqMB5Uqp2oiLynO0qSPjdbmyTEh1NVEM0fqQLiLMHsBbEOKNvqK+OYccDvdon7E1Kp1cAlK+XkBdeDSRzHcCiRJewGYmu7NUaQiSnGhI0mY+jMxIG0ruRlGXzv9/qgdMhKzZAc/sIqTCHPgxikfm3mqGwbGNxbEkDxRc9K1C8VOy3Cpjt4RqfnRmX1wKZJVKXJ0i3UmB1yxxd9n25o56CZJxu5S8Sd/Vd+/bTMlnyl64gl/ZVfAbcN3wQNPJmoW/wIDAQAB';
            //异步通知地址
            //编码格式
            $params['charset']              = "UTF-8";
            //签名方式
            $params['sign_type']            = "RSA2";
            //支付宝网关
            $params['gatewayUrl']           = "https://openapi.alipay.com/gateway.do";

        }

        $payModel = Pay::where('status', Pay::STATUS_BEGIN)
                    ->where('platform', $platform)
                    ->where('type', $type)
                    ->first();

        if (empty($payModel) || $payModel == null) {
            return self::error('请先开启支付');
        } 
        
        $payModel->params = serialize($params);
        $payModel->icon   = $request->input('icon') == null ? '' : $request->input('icon');
        $result = $payModel->save();

        return self::success('参数添加成功');

    }

    /**
     * 状态改变
     * @param  Request $request [description]
     * @return [type]           [description]
     */
    public function changeStatus(Request $request) {

        $class     = $request->input('class', 0);

        $status    = $request->input('status', 0);

        $platform  = $request->input('platform', 0);

        $type      = isset($this->payType[$class]) ? $this->payType[$class] : 0;

        $name      = isset($this->payName[$class]) ? $this->payName[$class] : 0;

        $payModel  = Pay::where('type', $type)->first();

        if (empty($payModel) || $payModel == null) {

            $payModel            = new Pay();
            $payModel->name      = $name;
            $payModel->type      = $type;
            $payModel->platform  = $platform;
        } 

        $payModel->status = $status;
        $result = $payModel->save();

        return ($result == true) ? self::success('成功') : self::error('失败');
    }


}
